package com.artwork.controller.api;

import com.artwork.common.domain.AjaxResult;
import com.artwork.common.enums.JsonResultEnum;
import com.artwork.common.exception.JsonException;
import com.artwork.model.Address;
import com.artwork.model.Sign;
import com.artwork.model.User;
import com.artwork.service.AddressService;
import com.artwork.service.SignService;
import com.artwork.service.UserService;
import com.artwork.utils.CodeUtil;
import com.artwork.utils.MD5Util;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author JoJo
 * @Data 2021/9/8$ 16:17$
 * @Description
 * @Param $
 * @Return $
 */
@RestController
@RequestMapping("/api")
public class UserApiController {
    @Autowired
    SignService signService;
    @Autowired
    UserService userService;

    @PostMapping("/addUser")
    public AjaxResult addUser(User user) throws JsonException{
        User user1 = userService.getUserByPhone(user.getPhone());
        if (user1 != null) {
            throw new JsonException(JsonResultEnum.USER_EXISTS);
        }
        user.setPassword(MD5Util.string2MD5(user.getPassword()));
        user.setCreateTime(System.currentTimeMillis());
        // 生成推广码YMARSN
        String code = "AZ09QM";
        User user2 = new User();
        do {
            code = CodeUtil.generateCode();
            user2 = userService.getUserByCode(code);
        } while (user2 != null);

        user.setCode(code);
        userService.addUser(user);
        return AjaxResult.success();
    }


    @GetMapping("/userInfo")
    public User getUserInfo(int id) throws JsonException{
        // todo 获取用户信息by: phone, username, id?

        return userService.getUserById(id);
    }

    /**
     * @desc 用户签到
     * @param id
     * @return
     * @throws JsonException
     */
    @PostMapping("/sign")
    public AjaxResult userSign(int id) throws JsonException {
        User user = userService.getUserById(id);
        if (user == null){
            throw new JsonException(JsonResultEnum.USER_NULL);
        }
        Sign sign = signService.getUserSign(id);
        // 初次签到 新增签到记录
        if (sign == null){
            signService.addSign(id);
        }else {
            // 判断今日是否签过到
            String today = DateFormatUtils.format(sign.getUpdateTime(), "yyyy-MM-dd");
            String date = DateFormatUtils.format(new Date(),"yyyy-MM-dd");
            if (date.equalsIgnoreCase(today)){
                return AjaxResult.error(304, "重复签到");
            }
            sign.setDays(sign.getDays() + 1);
            sign.setUpdateTime(System.currentTimeMillis());
            signService.updateSign(sign);
        }
        return AjaxResult.success();
    }
}
